import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/toast/brn_toast.dart';
import 'package:smart_devices/model/repair_order.dart';

class MineMaintainLogic extends GetxController {
  RxInt tabIndex = RxInt(0);

  RxInt total = RxInt(5);

  RxInt hours = RxInt(30);

  RxList<String> tabs = RxList(['本周', '上周', '30天内', '自定义']);

  RxList<RepairOrderModel> pageList = RxList([]);

  final ScrollController scrollController = ScrollController();

  RxBool isLoading = RxBool(false);

  @override
  void onInit() {
    super.onInit();
    initData();
    scrollController.addListener(() {
      if (!isLoading.value &&
          scrollController.position.pixels >= scrollController.position.maxScrollExtent) {
        isLoading.value = true;
        loadMoreData();
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.dispose();
  }

  initData() {
    for (var i = 0; i < 10; i++) {
      pageList.add(RepairOrderModel(
          title: '电梯设备维修02',
          orderNo: 'BX-2023041648',
          name: '电梯设备',
          code: 'SB0792789',
          location: '生产车间二层',
          degree: getDegreeByIndex(i),
          addTime: '2023.11.14 08:32:31',
          status: i % 2 == 0 ? RepairOrderEnum.fast : RepairOrderEnum.repair));
    }
  }

  Future<void> onRefresh(BuildContext context) {
    return Future.delayed(const Duration(seconds: 1), () {
      BrnToast.show('当前已是最新数据', context,
          gravity: BrnToastGravity.bottom, duration: const Duration(seconds: 2), radius: 10);
    });
  }

  RepairOrderDegreeEnum getDegreeByIndex(int index) {
    var mode = index % RepairOrderDegreeEnum.values.length;
    switch (mode) {
      case 0:
        return RepairOrderDegreeEnum.slow;
      case 1:
        return RepairOrderDegreeEnum.mid;
      default:
        return RepairOrderDegreeEnum.fast;
    }
  }

  Future<void> loadMoreData() {
    return Future.delayed(const Duration(seconds: 1), () {
      isLoading.value = false;
      initData();
    });
  }
}
